/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaRanking.java
 *
 * Created on 06-abr-2010, 1:06:02
 */

package poker;

import prop.Posicion;
import java.awt.Font;
import java.awt.Label;
import java.io.Serializable;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import prop.DriverDomainRanking;

/**
 *
 * @author pol
 */
public class VentanaRanking extends javax.swing.JFrame implements Serializable {
    private DriverDomainRanking ranking;
    private Label vLabelNombre[], vLabelFecha[], vLabelPuntos[];
    private Ventana padre;

    /** Creates new form VentanaRanking */
    public VentanaRanking(DriverDomainRanking rank, Ventana padre) {
        initComponents();

        ranking = rank;
        this.padre = padre;
        padre.setEnabled(false);

        Label tituloNombre = new Label();
        tituloNombre.setBounds(30, 20, 150, 20);
        tituloNombre.setText("Nombre");
        tituloNombre.setFont(new Font(null, Font.BOLD, 15));
        this.add(tituloNombre);

        Label tituloFecha = new Label();
        tituloFecha.setBounds(190, 20, 150, 20);
        tituloFecha.setText("Fecha");
        tituloFecha.setFont(new Font(null, Font.BOLD, 15));
        this.add(tituloFecha);

        Label tituloPuntos = new Label();
        tituloPuntos.setBounds(350, 20, 60, 20);
        tituloPuntos.setText("Puntos");
        tituloPuntos.setFont(new Font(null, Font.BOLD, 15));
        this.add(tituloPuntos);

        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);

        escribeRanking();
    }

    private void escribeRanking() {
        if (vLabelNombre != null) {
            for (int i = 0; i < vLabelNombre.length; ++i) {
                this.remove(vLabelNombre[i]);
                this.remove(vLabelFecha[i]);
                this.remove(vLabelPuntos[i]);
            }
        }
        Posicion[] vect = ranking.cargarRanking();
        int n = vect.length;
        vLabelNombre = new Label[n];
        vLabelFecha = new Label[n];
        vLabelPuntos = new Label[n];
        for (int i = 0; i < n; ++i) {
            vLabelNombre[i] = new Label();
            vLabelFecha[i] = new Label();
            vLabelPuntos[i] = new Label();

            vLabelNombre[i].setBounds(30, 50 + 25*i, 150, 20);
            vLabelFecha[i].setBounds(190, 50 + 25*i, 150, 20);
            vLabelPuntos[i].setBounds(350, 50 + 25*i, 60, 20);

            vLabelNombre[i].setText(vect[i].nombre);
            vLabelFecha[i].setText(vect[i].fecha);
            vLabelPuntos[i].setText(String.valueOf(vect[i].puntuacion));

            this.add(vLabelNombre[i]);
            this.add(vLabelFecha[i]);
            this.add(vLabelPuntos[i]);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botonResetear = new javax.swing.JButton();
        botonCerrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        botonResetear.setText("Resetear");
        botonResetear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonResetearActionPerformed(evt);
            }
        });

        botonCerrar.setText("Cerrar");
        botonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCerrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addComponent(botonResetear, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34)
                .addComponent(botonCerrar, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                .addGap(70, 70, 70))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(450, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonCerrar)
                    .addComponent(botonResetear))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCerrarActionPerformed
        this.dispose();
    }//GEN-LAST:event_botonCerrarActionPerformed

    private void botonResetearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonResetearActionPerformed
        int res = JOptionPane.showConfirmDialog(
                this,
                "Seguro que quieres resetear el ranking?",
                "Resetear ranking",
                JOptionPane.YES_NO_OPTION);
        if (res == JOptionPane.YES_OPTION) {
            ranking.resetear();
            escribeRanking();
        }
    }//GEN-LAST:event_botonResetearActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        padre.setEnabled(true);
    }//GEN-LAST:event_formWindowClosed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonCerrar;
    private javax.swing.JButton botonResetear;
    // End of variables declaration//GEN-END:variables

}
